/*
 * @Author: your name
 * @Date: 2022-01-06 17:57:34
 * @LastEditTime: 2022-03-25 17:44:13
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \web\src\router\login\index.jsx
 */
import React, { useState }from "react";
import "./index.css";
import request from "../../network/index";
import { Form, Input, Button, message, Radio } from "antd";
import Header from "../../components/header";
import Cookie from "../../assets/common/cookie"
import { useNavigate } from "react-router-dom";

const Login = () => {
  const [form] = Form.useForm();
  const navigate = useNavigate();
  const [type, setType] = useState(2);
  const onFinish = async values => {
    console.log("Success:", values);
    const res = await getLoginState({...values,type});
    if (res) {
      Cookie.remove("token");
      Cookie.set("token", res.token , '', 7);
      res.token = null;
      res.type = type;
      console.log('res',res);
      Cookie.set('userInfo', JSON.stringify(res));
      const url = type === '1' ? '/adminUserPage' : '/classPage';
      navigate(url);
      message.success("登陆成功!", 1.5);
    } else {
      form.resetFields();
    }
  };
  const selected = (e) => {
    setType(e.target.value)
  };
  const getLoginState = async (values) => {
    let res = await request.post("login", values);
   
    return res ? res : null;
  };

  return (
    <div className="login">
      <Header />
      <div className="loginCard">
        <div className="loginCard-logo"></div>
        <div className="select">
          <Radio.Group defaultValue="2" buttonStyle="solid" onChange={selected}>
            <Radio.Button value="2">老师</Radio.Button>
            <Radio.Button value="3">学生</Radio.Button>
            <Radio.Button value="1">管理员</Radio.Button>
          </Radio.Group>
        </div>
        <Form
          form={form}
          name="basic"
          labelCol={{ span: 6 }}
          wrapperCol={{ span: 16 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          autoComplete="off"
        >
          <Form.Item
            label="账号"
            name="sole"
            rules={[{ required: true, message: "请输入你的账号!" }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="密码"
            name="password"
            rules={[{ required: true, message: "请输入你的密码!" }]}
          >
            <Input.Password />
          </Form.Item>
          {/* <Form.Item
            name="remember"
            valuePropName="checked"
            wrapperCol={{ offset: 10, span: 16 }}
          >
            <Checkbox>记住我</Checkbox>
          </Form.Item> */}

          <Form.Item wrapperCol={{ offset: 10, span: 16 }}>
            <Button type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

export default Login;